package com.lhdx.practical.service.impl;

import com.lhdx.practical.bean.Content;
import com.lhdx.practical.bean.Task;
import com.lhdx.practical.dao.TaskDao;
import com.lhdx.practical.service.TaskService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service("TaskService")
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskDao taskDao;

    @Override
    public void save(Task task) {
        taskDao.save(task);
    }

    @Override
    public void deleteById(long id) {
        taskDao.deleteById(id);
    }

    @Override
    public Task findById(long id) {
        return taskDao.findById(id);
    }

    @Override
    public List<Task> findAllByAuthoridAndTypeAndSidAndMidAndGradeid(long authorid,int type,long sid,long mid,long gradeid) {
        return taskDao.findAllByAuthoridAndTypeAndSidAndMidAndGradeid(authorid,type,sid,mid,gradeid);
    }

    @Override
    public List<Task> findAllByTypeAndSidAndMidAndGradeid(int type, long sid, long mid, long gradeid) {
        return taskDao.findAllByTypeAndSidAndMidAndGradeid(type,sid,mid,gradeid);
    }

    @Override
    public Page<Task> getResultList(long mid, long sid,long gradeid, int type, int page, int pagesize) {
        Sort sort = new Sort(Sort.Direction.ASC, "createtime");
        Pageable pageable = PageRequest.of(page == 0 ? 0 : page -1, pagesize, sort);
        Page<Task> data = taskDao.findAll(new Specification<Task>(){
            @Override
            public Predicate toPredicate(Root<Task> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(cb.equal(root.get("mid"),mid));
                list.add(cb.equal(root.get("sid"),sid));
                list.add(cb.equal(root.get("type"),type));
                list.add(cb.equal(root.get("gradeid"),gradeid));
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
        return data;
    }

    @Override
    public List<Task> findAllByGroupidAndType(long groupid,int type){
        return taskDao.findAllByGroupidAndType(groupid,type);
    }

    @Override
    public List<Task> findAllByGradeidAndType(long gradeid, int type) {
        return taskDao.findAllByGradeidAndType(gradeid,type);
    }

    @Override
    public int countAllBySidAndType(long sid, int type) {
        return taskDao.countAllBySidAndType(sid,type);
    }
}
